//页面加载完成执行里面的方法
$(function () {
    $("#page").load("/html/back/page.html");
});
//对象的格式aa=｛key:value,key1:value1｝;aa.key,aa.key1
//data={result:1,list:array(35)} data.list
function findAll() {
    let data = {
        username: $("#username").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        sex: $("#sex").val(),
        hobby: getHobby().toLocaleString(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        curPage: $("#curPage").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data);
    $.ajax({
        url: '/back/user/findAll',
        data: data,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            $("#countSize").html(data.countSize);
            setUserData(data.list);
        }
    });
}

function getHobby() {
    let vals = [];
    $('input:checkbox[name=hobby]:checked').each(function (index, item) {
        vals.push($(this).val());
    });
    return vals;
}

function setUserData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<div>\n' +
            '            <div>' + data[i].username + '</div>\n' +
            '            <div>' + getUndefined(data[i].email) + '</div>\n' +
            '            <div>' + getUndefined(data[i].phone) + '</div>\n' +
            '            <div>' + data[i].sex + '</div>\n' +
            '            <div>' + getUndefined(data[i].hobby) + '</div>\n' +
            '            <div>' + data[i].birthday.split(" ")[0] + '</div>\n' +
            '            <div>' + (data[i].enable == 1 ? "正常" : "异常") + '</div>\n' +
            '            <div>\n' +
            '                <div onclick="toEditPage(' + data[i].id + ')"><i class="iconfont icon-xxx">&#xe626;</i></div>\n' +
            '                <div onclick="del(' + data[i].id + ')"><i class="iconfont icon-xxx">&#xe624;</i></div>\n' +
            '                <div onclick="editPassword(' + data[i].id + ')"><i class="iconfont icon-xxx">&#xe61e;</i></div>\n' +
            '            </div>\n' +
            '        </div>';
    }
    $(".my-body").html(html);
}

function toEditPage(id) {
    //session  local
    localStorage.setItem("userId", id);
    sessionStorage.setItem("userId", id);
    openWindow("/html/back/user/userEdit.html");
}

function getUndefined(value) {
    return value == 'undefined' || value == 'null' || value == null ? '' : value;
}

function del(id) {
    $.ajax({
        url: '/back/user/del',
        data: {id: id},
        type: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            if (data.result == 'success') {
                findAll();
            } else {
                alert("删除失败，请联系维护人员");
            }
        }
    });
}
